import cv2
import numpy as np

# 加载图像
image_path = "/home/hw/dataset/my_dataset1/images/184.jpg"  # 替换为你的实际图片路径
img = cv2.imread(image_path)

# 获取图像的高度和宽度
height, width, _ = img.shape

# 创建一个比原图稍大的新图像，用于添加坐标轴
margin = 80  # 边距大小
new_height = height + 2 * margin
new_width = width + 2 * margin
new_img = np.ones((new_height, new_width, 3), dtype=np.uint8) * 255

# 将原图粘贴到新图像中央
new_img[margin : margin + height, margin : margin + width] = img

# 绘制归一化坐标轴
for i in range(11):
    x = margin + int(i * width / 10)
    y = margin + int(i * height / 10)
    cv2.putText(
        new_img,
        f"{i/10:.1f}",
        (x - 15, margin - 10),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.5,
        (0, 0, 0),
        1,
        cv2.LINE_AA,
    )
    cv2.putText(
        new_img,
        f"{i/10:.1f}",
        (margin - 35, y + 5),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.5,
        (0, 0, 0),
        1,
        cv2.LINE_AA,
    )

# 计算y=0.05的位置
y_line = margin + int(height * 0.05)

# 在图像上绘制红线
cv2.line(
    new_img,
    (margin, y_line),
    (margin + width, y_line),
    (0, 0, 255),
    2,
    lineType=cv2.LINE_AA,
)

# 在y=0.05处标注0.05
cv2.putText(
    new_img,
    "0.05",
    (margin - 40, y_line + 5),
    cv2.FONT_HERSHEY_SIMPLEX,
    0.5,
    (0, 0, 255),
    1,
    cv2.LINE_AA,
)

# 添加红色的坐标系方向示意标志
arrow_length = int(width * 0.1)  # 箭头长度相对于图像宽度的10%
cv2.arrowedLine(
    new_img,
    (margin - 50, margin - 50),
    (margin - 50 + arrow_length, margin - 50),
    (0, 0, 0),
    2,
    tipLength=0.1,
)
cv2.arrowedLine(
    new_img,
    (margin - 50, margin - 50),
    (margin - 50, margin - 50 + arrow_length),
    (0, 0, 0),
    2,
    tipLength=0.1,
)
cv2.putText(
    new_img,
    "X",
    (margin - 50 + arrow_length + 10, margin - 50),
    cv2.FONT_HERSHEY_SIMPLEX,
    0.6,
    (0, 0, 0),
    2,
)
cv2.putText(
    new_img,
    "Y",
    (margin - 50, margin - 50 + arrow_length + 20),
    cv2.FONT_HERSHEY_SIMPLEX,
    0.6,
    (0, 0, 0),
    2,
)

# 保存并显示图像
output_path = "image_with_normalized_axes_and_line.jpg"
cv2.imwrite(output_path, new_img)

# 显示图像
cv2.imshow("Image with Normalized Axes and Line", new_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
